/*
 * HSM Proxy Project.
 * Copyright (C) 2013 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see 
 * http://www.gnu.org/licenses/.
 */

package be.fedict.hsm.admin.webapp;

import java.io.Serializable;

import javax.enterprise.context.Conversation;
import javax.enterprise.context.SessionScoped;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

@Named("hsmProxyMenu")
@SessionScoped
public class MenuController implements Serializable {

	private static final long serialVersionUID = 1L;

	@Inject
	private Conversation conversation;

	public String getMenuitemStyleClass(String action) {
		String viewId = getViewId();
		if (null == viewId) {
			return "";
		}
		if (viewId.startsWith(action)) {
			return "ui-state-active";
		}
		return "";
	}

	private String getViewId() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UIViewRoot viewRoot = facesContext.getViewRoot();
		String viewId = viewRoot.getViewId();
		return viewId;
	}

	public String nav(String viewId) {
		if (false == this.conversation.isTransient()) {
			this.conversation.end();
		}
		return viewId;
	}
}
